import {createRouter, createWebHashHistory} from 'vue-router'

class MetaInfo {
    title;
    desc;
    modalWidth;

    constructor(title) {
        this.title = title;
    }

    /**
     * 添加详情
     */
    setDesc(desc) {
        this.desc = desc;
        return this;
    }

    /**
     * 设置窗口宽度
     */
    setModalWidth(width) {
        this.modalWidth = width;
        return this;
    }
}

class RouteItem {
    path;
    name;
    meta;
    component;
    children;
    parent;
    fullPath;
    modal = true;

    constructor(path, name, component, meta) {
        this.path = path;
        this.fullPath = path;
        this.name = name;
        this.component = component;
        this.meta = meta;
    }

    /**
     * 添加额外属性
     */
    setMeta(meta) {
        this.meta = meta;
        return this;
    }

    /**
     * 是否开启多窗口
     * @param modal {boolean} 是否开启
     */
    setModal(modal) {
        this.modal = modal;
        return this;
    }

    /**
     * 关闭多窗口
     */
    closeModal() {
        this.modal = false;
        return this;
    }

    /**
     * 添加子路由
     */
    setChildren(childrens) {
        childrens.map(item => item.parent = this);
        this.children = childrens;
        return this;
    }

    buildFullPathFun(routeItem) {
        if (routeItem.parent) {
            routeItem.fullPath = routeItem.parent.fullPath + "/" + routeItem.path;
        }
        if (routeItem.children) {
            for (let child of routeItem.children) {
                this.buildFullPathFun(child);
            }
        }
    }

    /**
     * 构建全路径
     */
    buildFullPath() {
        if (this.children) {
            for (let child of this.children) {
                this.buildFullPathFun(child);
            }
        }
        return this;
    }

    clearCircularFun(routeItem) {
        if (routeItem.children) {
            for (let child of routeItem.children) {
                this.clearCircularFun(child);
            }
        }
        if (routeItem.parent) {
            const newParent = {};
            for (let key of Object.keys(routeItem.parent)) {
                if (key !== 'children' && key !== 'parent') {
                    newParent[key] = routeItem.parent[key];
                }
            }
            routeItem.parent = newParent;
        }
    }

    /**
     * 清除掉互相引用，往往用于转JSON前
     */
    clearCircular() {
        if (this.children) {
            for (let child of this.children) {
                this.clearCircularFun(child);
            }
        }
        if (this.parent) {
            this.parent = JSON.parse(JSON.stringify(this.parent));
        }
        return this;
    }
}

const routes = [
    new RouteItem("/", "Home", () => import('../views/Home'), new MetaInfo("首页")),
    new RouteItem("/redirect", "Redirect", () => import('../views/Redirect'), new MetaInfo("跳转")),
    new RouteItem("/toolPages", "ToolPages")
        .setChildren([
            new RouteItem('generate', 'Generate')
                .setMeta(new MetaInfo('生成工具'))
                .setChildren([
                    new RouteItem("randPhone", "RandPhone", () => import('../views/toolPages/generate/RandPhone'))
                        .setMeta(new MetaInfo("随机手机号")
                            .setDesc("生成随机的手机号")),
                    new RouteItem("randPlate", "RandPlate", () => import('../views/toolPages/generate/RandPlate'))
                        .setMeta(new MetaInfo("随机车牌号")
                            .setDesc("生成随机的车牌号，可自定义区域")),
                    new RouteItem("randAlphanumeric", "RandAlphanumeric", () => import('../views/toolPages/generate/RandAlphanumeric'))
                        .setMeta(new MetaInfo("随机数字字母串")
                            .setDesc("生成随机数字字母串，可自定义长度")),
                    new RouteItem("randFullName", "RandFullName", () => import('../views/toolPages/generate/RandFullName'))
                        .setMeta(new MetaInfo("随机姓名")
                            .setDesc("生成随机的姓名")),
                    new RouteItem("randomIDCard", "RandIDCard", () => import('../views/toolPages/generate/RandIDCard'))
                        .setMeta(new MetaInfo("随机身份证号")
                            .setDesc("生成随机的身份证号，可指定地区")),
                    new RouteItem("randBankNumber", "RandBankNumber", () => import('../views/toolPages/generate/RandBankNumber'))
                        .setMeta(new MetaInfo("随机银行卡号")
                            .setDesc("生成随机的随机银行卡号，可选择指定银行")),
                    new RouteItem("generateUUID", "GenerateUUID", () => import('../views/toolPages/generate/GenerateUUID'))
                        .setMeta(new MetaInfo("生成UUID")
                            .setDesc("生成UUID（通用唯一识别码）")),
                    new RouteItem("randNumber", "RandNumber", () => import('../views/toolPages/generate/RandNumber'))
                        .setMeta(new MetaInfo("生成随机数")
                            .setDesc("生成随机数，可指定范围、保留小数位")),
                    new RouteItem("randDate", "RandDate", () => import('../views/toolPages/generate/RandDate'))
                        .setMeta(new MetaInfo("生成随机日期")
                            .setDesc("生成从1970年1月1日到现在的随机日期，可自定义生成范围")),
                    new RouteItem("generateStringFromRegex", "GenerateStringFromRegex", () => import('../views/toolPages/generate/GenerateStringFromRegex'))
                        .setMeta(new MetaInfo("根据正则生成字符串")
                            .setDesc("根据正则表达式生成字符串")),
                    new RouteItem("generateUnifiedSocialCreditIdentifier", "GenerateUnifiedSocialCreditIdentifier", () => import('../views/toolPages/generate/GenerateUnifiedSocialCreditIdentifier'))
                        .setMeta(new MetaInfo("随机社会信用代码")
                            .setDesc("生成随机的统一社会信用代码")),
                    new RouteItem("generateMyBatis", "GenerateMyBatis", () => import('../views/toolPages/generate/GenerateMyBatis.vue'))
                        .setMeta(new MetaInfo("myBatis")
                            .setDesc("生成MyBatis、实体类、ts等常用的有关sql字段的数据")),
                    new RouteItem("generateCron", "GenerateCron", () => import('../views/toolPages/generate/GenerateCron.vue'))
                        .setMeta(new MetaInfo("定时任务表达式")
                            .setDesc("生成定时任务的cron表达式或反解析")),
                ]),
            new RouteItem('dateTime', 'DateTime')
                .setMeta(new MetaInfo('日期工具'))
                .setChildren([
                    new RouteItem("compareDateTime", "CompareDateTime", () => import('../views/toolPages/dateTime/CompareDateTime'))
                        .setMeta(new MetaInfo("计算两个日期间隔")
                            .setDesc("输入两个日期，计算两个日期的间隔时长")),
                    new RouteItem("formatDate", "FormatDate", () => import('../views/toolPages/dateTime/FormatDate'))
                        .setMeta(new MetaInfo("格式化日期")
                            .setDesc("输入任意时间格式文字，即可格式化成指定日期格式")),
                    new RouteItem("chineseDate", "ChineseDate", () => import('../views/toolPages/dateTime/ChineseDate'))
                        .setMeta(new MetaInfo("农历日期")
                            .setDesc("选择一个公历日期，获得农历名、干支、生肖、节气、节日、朝代等信息")),
                    new RouteItem("chineseToDate", "ChineseToDate", () => import('../views/toolPages/dateTime/ChineseToDate'))
                        .setMeta(new MetaInfo("农历转公历")
                            .setDesc("选择一个农历转为公历日期")),
                ]),
            new RouteItem('stringDeal', 'StringDeal')
                .setMeta(new MetaInfo('字符串处理工具'))
                .setChildren([
                    new RouteItem("randCutString", "RandCutString", () => import('../views/toolPages/stringDeal/RandCutString'))
                        .setMeta(new MetaInfo("截取随机字符")
                            .setDesc("输入一段文字，随机截取一段字符")),
                    new RouteItem("JSONPretty", "JSONPretty", () => import('../views/toolPages/stringDeal/JSONPretty'))
                        .setMeta(new MetaInfo("JSON 格式化")
                            .setDesc("输入 JSON 数据，可进行格式化、校验、压缩、修复等处理")),
                    new RouteItem("stringDiff", "StringDiff", () => import('../views/toolPages/stringDeal/StringDiff'))
                        .setMeta(new MetaInfo("字符串比较")
                            .setDesc("输入两段字符串，比较不同，类似于 git 代码提交的比较")
                            .setModalWidth(1000)),
                ]),
            new RouteItem('qrAndBar', 'qrAndBar')
                .setMeta(new MetaInfo('二维码和条形码'))
                .setChildren([
                    new RouteItem("qrGenerate", "QrGenerate", () => import('../views/toolPages/qrAndBarCode/QrGenerate.vue'))
                        .setMeta(new MetaInfo("二维码")
                            .setDesc("生成二维码")),
                ]),
            new RouteItem('infoQuery', 'InfoQuery')
                .setMeta(new MetaInfo('信息查询工具'))
                .setChildren([
                    new RouteItem("areaCodeQuery", "AreaCodeQuery", () => import('../views/toolPages/infoQuery/AreaCodeQuery'))
                        .setMeta(new MetaInfo("区域编码查询")
                            .setDesc("查询区域的编码")),
                ]),
            new RouteItem('picture', 'Picture')
                .setMeta(new MetaInfo('图片工具'))
                .setChildren([
                    new RouteItem("pictureCropper", "PictureCropper", () => import('../views/toolPages/picture/PictureCropper.vue'))
                        .setMeta(new MetaInfo("图片裁剪")
                            .setDesc("选择图片进行任意裁剪"))
                        .closeModal(),
                    new RouteItem("photoShop", "PhotoShop", () => import('../views/toolPages/picture/PhotoShop.vue'))
                        .setMeta(new MetaInfo("在线 PhotoShop")
                            .setDesc("在线修改图片"))
                        .closeModal(),
                ]),
            new RouteItem('graphCalculator', 'GraphCalculator')
                .setMeta(new MetaInfo('图形计算器'))
                .setChildren([
                    new RouteItem("calculator", "Calculator", () => import('../views/toolPages/graphCalculator/GraphCalculator.vue'))
                        .setMeta(new MetaInfo("图形计算器")
                            .setDesc("在线函数图形计算器，类似于 Desmos"))
                        .closeModal(),
                ]),
            new RouteItem('other', 'Other')
                .setMeta(new MetaInfo('其他'))
                .setChildren([
                    new RouteItem("wallpaper", "Wallpaper", () => import('../views/toolPages/wallpaper/Wallpaper.vue'))
                        .setMeta(new MetaInfo("壁纸")
                            .setDesc("壁纸"))
                        .closeModal(),
                ]),
            // new RouteItem('encryption', 'Encryption')
            //     .setMeta(new MetaInfo('加密'))
            //     .setChildren([
            //         new RouteItem('MD5', 'MD5', () => import('../views/toolPages/encryption/Md5.vue'))
            //             .setMeta(new MetaInfo('MD5加密')
            //                 .setDesc('MD5加密'))
            //     ])
        ]).buildFullPath(),

]

const router = createRouter({
    history: createWebHashHistory(process.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        // 路由跳转刷新滚动条位置
        return {x: 0, y: 0}
    }
})

export {
    routes
}

export default router
